package cn.gson.boot.controller;

import cn.gson.boot.model.dao.KehuDao;
import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.KehuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/kehu")
public class KehuController {
    @Autowired
    KehuService kehuService;

    //分页查询客户
    @RequestMapping("/kehu-list")
    @ResponseBody
    public Map<String,Object> kehuList(String kType,String kName,String kSmzq,Integer pageNum,Integer pageSize,HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        List<Kehu> kehulist = kehuService.allKehus(kType,kName,kSmzq,empId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",kehulist);
        return map;
    }

    //查客户连表客户联系人和员工详情查询
    @RequestMapping("/kehu-xqlist")
    @ResponseBody
    public List<Kehu> xqKehus(Integer kId){
        return kehuService.xqKehus(kId);
    }

    //客户详情查询
    @RequestMapping("/kehu-xqlist2")
    @ResponseBody
    public Kehu xq2Kehus(Integer kId){
        return kehuService.xq2Kehus(kId);
    }

    //查询所有客户回收站
    @RequestMapping("/kehu-huishoulist")
    @ResponseBody
    public List<Kehu> Kehushui(){
        return kehuService.Kehushui();
    }

    //查询所有客户
    @RequestMapping("/kehu-suoylist")
    @ResponseBody
    public List<KehuVo> suoyoukehu(){
        return kehuService.suoyoukehu();
    }

    //查询客户名称是否重复
    @RequestMapping("/kehu-shifoukehu")
    @ResponseBody
    public int  kehuchongfu(String kName){
         return kehuService.kehuchongfu(kName);
    }

    //中国地图查询数据
    @RequestMapping("/kehu-ditulist")
    @ResponseBody
    public List<Kehu> dituKehus(HttpSession session){
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        return kehuService.dituKehus(empId);
    }

    //时间线--查询全部
    @RequestMapping("/kehu-quanbulist")
    @ResponseBody
    public List<Kehu> timeSelect(Integer kid){
        return kehuService.timeSelect(kid);
    }

    //时间线--销售机会
    @RequestMapping("/kehu-saleslist")
    @ResponseBody
    public List<Sales> allshangji(Integer kId){
        return  kehuService.allshangji(kId);
    }

    //时间线--客户关怀
    @RequestMapping("/kehu-caringlist")
    @ResponseBody
    public List<Caring> allcaring(Integer kId){
        return  kehuService.allcaring(kId);
    }

    //时间线--维修工单
    @RequestMapping("/kehu-weixiulist")
    @ResponseBody
    public List<Weixiugongdan> allwxiu(Integer kId){
        return kehuService.allwxiu(kId);
    }

    //时间线--合同
    @RequestMapping("/kehu-hetonglist")
    @ResponseBody
    public List<Hetong> allhetong(Integer kId){
        return kehuService.allhetong(kId);
    }

    //时间线--回款
    @RequestMapping("/kehu-huikuanlist")
    @ResponseBody
    public List<Huikuan> allhuikuan(Integer kId){
        return kehuService.allhuikuan(kId);
    }

    //时间线--退货
    @RequestMapping("/kehu-tuihuolist")
    @ResponseBody
    public List<Tuihuo> alltuihuo(Integer kId){
        return kehuService.alltuihuo(kId);
    }

    //时间线--交付记录
    @RequestMapping("/kehu-jiaofujllist")
    @ResponseBody
    public List<Jiaofujilu> alljiaofujlu(Integer kId){
        return kehuService.alljiaofujlu(kId);
    }

    //新增客户
    @RequestMapping("/add-kehu")
    @ResponseBody
    public  String addKehu(@RequestBody KehuandConVo kehuandConVo,HttpSession session){
        String addkehu = kehuService.addKehu(kehuandConVo,session);
        return addkehu;
    }

    //获客界面新增客户
    @RequestMapping("/add-huokehu")
    @ResponseBody
    public String addhuokehu(@RequestBody KehuandConVo kehuandConVo , HttpSession session){
        String addhuokehu = kehuService.addhuokehu(kehuandConVo, session);
        return addhuokehu;
    }

    //导入新增客户
    @RequestMapping("/add-daorkehu")
    @ResponseBody
    public String addCustomer(String kehu,HttpSession session) throws ParseException {
        return kehuService.importCustomer(kehu,session);
    }

    //逻辑删除
    @RequestMapping("/delete-kehu")
    @ResponseBody
    public void  deleteKehu(int id ){
        kehuService.deleteKehu(id);
    }

    //还原客户
    @RequestMapping("/huanyuan-kehu")
    @ResponseBody
    public void  huanyuan(Integer id,HttpSession session ){
        kehuService.huanyuan(id,session);
    }

    //客户合并
    @RequestMapping("/hebing-kehu")
    @ResponseBody
    public void Kehuhebing(Integer Aid,Integer Bid,HttpSession session ){
        kehuService.Kehuhebing(Aid,Bid,session);
    }

    //统计图客户类型查询
    @RequestMapping("/kehu-lxchaxu")
    @ResponseBody
    public Map<String,Object> kehuleixing(String vv,HttpSession session){
        Map<String,Object> map = null;
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        if(vv.equals("客户类型数量统计")) {
            map = kehuService.kehuleixing(empId);
        }else if(vv.equals("客户创建数量月份统计")){
            map=kehuService.kehucj(empId);
        }else{
            map = kehuService.kehufuzeren(empId);
        }
        return map;
    }
}
